<template>
  <div class="ele-body">
    <el-card shadow="never">

      <!-- 搜索表单 -->
      <el-popover ref="search" placement="bottom" offset="0" @hide="clearForm" :width="400">
      <el-form :model="table.where" label-width="90px" class="ele-form-search"
               @keyup.enter.native="$refs.facultyTable.reload()" @submit.native.prevent>
        <el-row>
          <el-col>
            <el-form-item label="教职工名称:">
              <el-input v-model="table.where.name" placeholder="请输入教职工名称" clearable/>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="性别：" prop="gender">
              <el-select v-model="table.where.gender" placeholder="请选择性别" class="ele-block" clearable>
                <el-option label="男" :value="1"/>
                <el-option label="女" :value="2"/>
                <el-option label="未知" :value="3"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="是否绑定小程序:">
              <el-input v-model="table.where.is_bind" placeholder="请输入是否绑定小程序" clearable/>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item>
              <div class="ele-form-actions">
                <el-button type="primary" @click="$refs.facultyTable.reload()" icon="el-icon-search" class="ele-btn-icon">查询
                </el-button>
                <el-button @click="(table.where={})&&$refs.facultyTable.reload()">重置</el-button>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      </el-popover>

      <!-- 操作按钮 -->
      <div class="ele-table-tool ele-table-tool-default">
        <el-button v-popover:search type="primary" icon="el-icon-plus" class="ele-btn-icon" size="small">查询</el-button>
        <el-button @click="showAddFacultyForm=true" type="primary" icon="el-icon-plus" class="ele-btn-icon" size="small">新增教职工</el-button>
        <el-button @click="remove()" type="danger" icon="el-icon-delete" class="ele-btn-icon" size="small">批量删除</el-button>
        <el-button @click="showCreateDataImportForm=true" type="primary" icon="el-icon-plus" class="ele-btn-icon" size="small">批量入库</el-button>
      </div>

      <!-- 数据表格 -->
      <ele-data-table ref="facultyTable" :config="table" :choose.sync="choose" height="calc(100vh - 315px)" highlight-current-row>
        <template slot-scope="{index}">
          <el-table-column type="selection" width="45" align="center" fixed="left"/>
          <el-table-column type="index" :index="index" label="编号" width="60" align="center" fixed="left" show-overflow-tooltip/>

          <el-table-column prop="class_id" label="照片" sortable="custom" show-overflow-tooltip min-width="120">
            <template slot-scope="{row}">
              <el-image
                  style="width: 100px; height: 100px"
                  :src="row.face_url"
                  :preview-src-list="row.origin_face_url">
              </el-image>
            </template>
          </el-table-column>

          <el-table-column prop="" label="人脸检测" sortable="custom" show-overflow-tooltip min-width="120"/>

          <el-table-column prop="name" label="教职工名称" sortable="custom" show-overflow-tooltip min-width="120"/>
                                
          <el-table-column prop="gender" label="性别" sortable="custom" show-overflow-tooltip min-width="120"/>
                                
          <el-table-column prop="mobile" label="联系电话" sortable="custom" show-overflow-tooltip min-width="120"/>

          <el-table-column prop="ic_card" label="考勤卡号" sortable="custom" show-overflow-tooltip min-width="120"/>
                                
          <el-table-column prop="idcard_type" label="证件类型" sortable="custom" show-overflow-tooltip min-width="120"/>
                                
          <el-table-column prop="idcard" label="证件号" sortable="custom" show-overflow-tooltip min-width="120"/>
                                
          <el-table-column prop="" label="角色" sortable="custom" show-overflow-tooltip min-width="120"/>
                                          
          <el-table-column prop="is_bind" label="是否绑定小程序" sortable="custom" :resizable="false" min-width="150">
              <template slot-scope="{row}">
                  <el-switch v-model="row.is_bind" @change="setIsBind(row)" :active-value="1" :inactive-value="2"/>
              </template>
          </el-table-column>

          <el-table-column label="操作" width="130px" align="center" :resizable="false"  fixed="right">
            <template slot-scope="{row}">
              <el-link @click="edit(row)" icon="el-icon-edit" type="primary" :underline="false">修改</el-link>
              <el-popconfirm title="确定要删除此教职工吗？" @confirm="remove(row)" class="ele-action">
                <el-link slot="reference" icon="el-icon-delete" type="danger" :underline="false">删除</el-link>
              </el-popconfirm>
            </template>
          </el-table-column>
        </template>
      </ele-data-table>
    </el-card>

    <create-faculty :showAddFacultyForm.sync="showAddFacultyForm"></create-faculty>
    <create-data-import :showCreateDataImportForm.sync="showCreateDataImportForm"></create-data-import>

  </div>
</template>

<script>
import CreateFaculty from './create';
import CreateDataImport from "../student/create-data-import";
export default {
  name: "SchoolFaculty",
  components: {CreateFaculty, CreateDataImport},
  data() {
    return {
      table: {url: '/worker/list', where: {}},  // 表格配置
      choose: [],  // 表格选中数据
      showAddFacultyForm: false, // 显示添加教职工弹窗
      showCreateDataImportForm: false, // 是否显示创建数据导入弹窗
      editForm: {},  // 表单数据
      editRules: {  // 表单验证规则
                        
        name: [
          {required: true, message: '请输入教职工名称', trigger: 'blur'}
        ],
                          
        gender: [
          {required: true, message: '请选择性别', trigger: 'blur'}
        ],
                                    
        number: [
          {required: true, message: '请输入工号', trigger: 'blur'}
        ],
                          
        mobile: [
          {required: true, message: '请输入联系电话', trigger: 'blur'}
        ],
                          
        birthday: [
          {required: true, message: '请输入生日', trigger: 'blur'}
        ],
                          
        idcard_type: [
          {required: true, message: '请选择证件类型', trigger: 'blur'}
        ],
                                    
        idcard: [
          {required: true, message: '请输入证件号', trigger: 'blur'}
        ],
                          
        census_register: [
          {required: true, message: '请输入户籍', trigger: 'blur'}
        ],
                                    
        address: [
          {required: true, message: '请输入住址', trigger: 'blur'}
        ],
                          
        is_bind: [
          {required: true, message: '请选择是否绑定小程序', trigger: 'blur'}
        ],
                          
        sort: [
          {required: true, message: '请输入排序', trigger: 'blur'}
        ],
              
      },
    }
  },
  mounted() {
  },
  methods: {
    /**
     * 显示编辑
     */
    edit(row) {
      this.$http.get('/worker/info?id=' + row.id).then(res => {
        this.editForm = res.data;
        this.showEdit = true;
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /**
     * 保存编辑
     */
    save() {
      this.$refs['editForm'].validate((valid) => {
        if (valid) {
          const loading = this.$loading({lock: true});
          this.$http.post('/worker/edit', this.editForm).then(res => {
            loading.close();
            this.showEdit = false;
            this.$message({type: 'success', message: res.msg});
            this.$refs.facultyTable.reload();
          }).catch(e => {
            loading.close();
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /**
     * 刪除(批量刪除)
     */
    remove(row) {
      console.log(row)
      if (!row) {  // 批量删除
        if (this.choose.length === 0) return this.$message.error('请至少选择一条数据');
        let ids = this.choose.map(d => d.id);
        this.$confirm('确定要删除选中的教职工吗?', '提示', {type: 'warning'}).then(() => {
          const loading = this.$loading({lock: true});
          this.$http.post('/worker/destroy', {id: ids}).then(res => {
            loading.close();
            this.$message({type: 'success', message: res.msg});
            this.$refs.facultyTable.reload();
          }).catch(e => {
            loading.close();
            this.$message.error(e.message);
          });
        }).catch(() => 0);
      } else {  // 单个删除
        const loading = this.$loading({lock: true});
        this.$http.post('/worker/destroy', {id:row.id}).then(res => {
          loading.close();
          this.$message({type: 'success', message: res.msg});
          this.$refs.facultyTable.reload();
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }
    },
                                          
    /**
     * 更改是否绑定小程序
     */
    setIsBind(row) {
        const loading = this.$loading({lock: true});
        let params = Object.assign({}, row);
        this.$http.post("/worker/setIsBind", params).then(() => {
            loading.close();
            this.$message({type: 'success', message: '操作成功'});
        }).catch(e => {
            loading.close();
            row.is_bind = !row.is_bind ? 2 : 1;
            this.$message.error(e.message);
        });
    },
    /**
     * 清理查询表单
     */
    clearForm() {
      this.searchForm = {};
    }
  }
}
</script>

<style scoped>
.ele-block >>> .el-upload, .ele-block >>> .el-upload-dragger {
  width: 100%;
}
</style>